<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="24">
                <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select
                    placeholder="选择供应商"
                    v-model="queryParam.organId"
                    :dropdownMatchSelectWidth="false"
                    showSearch
                    allow-clear
                    optionFilterProp="children">
                    <a-select-option v-for="(item,index) in supList" :key="index" :value="item.id">
                      {{ item.supplier }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="账单周期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-range-picker
                    style="width: 100%"
                    v-model="queryParam.createTimeRange"
                    format="YYYY-MM-DD"
                    :placeholder="['开始时间', '结束时间']"
                    @change="onDateChange"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery">查询</a-button>
                  <a-button style="margin-left: 8px" @click="importXls()">导入</a-button>
                  <!-- <a-button style="margin-left: 8px" v-print="'#reportPrint'" icon="printer">打印</a-button>
                  <a-button style="margin-left: 8px" @click="exportExcel" icon="download">导出</a-button> -->
                </span>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item>
                  <!-- {{ firstTotal }} {{ lastTotal }} -->
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <div class="table-operator" style="margin-top: 5px">
          <a-button @click="updateStatement" type="primary" icon="update">更新对账单</a-button>
          <a-button @click="handleImportXls()" icon="import">导入</a-button>
          <a-button @click="handleExportXls('采购对账单')" icon="download">导出</a-button>
        </div>
        <!-- table区域-begin -->
        <section ref="print" id="reportPrint">
          <a-table
            bordered
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :components="handleDrag(columns)"
            :pagination="false"
            :scroll="scroll"
            :loading="loading"
            :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
            @change="handleTableChange">
            <span slot="action" slot-scope="text, record">
              <a-divider v-if="record.id" type="vertical" />
              <a v-if="record.statisticalType !='系统计算'" @click="handleEdit(record)">编辑</a>
              <a-divider v-if="record.id" type="vertical" />
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record)">
                <a v-if="record.statisticalType !='系统计算'">删除</a>
              </a-popconfirm>
            </span>
            <span slot="customTitle">
              期末应付
              <a-tooltip title="期末应付=期初应付+本期欠款-本期付款">
                <a-icon type="question-circle" />
              </a-tooltip>
            </span>
          </a-table>
          <a-row :gutter="24" style="margin-top: 8px;text-align:right;">
            <a-col :md="24" :sm="24">
              <a-pagination
                @change="paginationChange"
                @showSizeChange="paginationShowSizeChange"
                size="small"
                show-size-changer
                :showQuickJumper="true"
                :current="ipagination.current"
                :page-size="ipagination.pageSize"
                :page-size-options="ipagination.pageSizeOptions"
                :total="ipagination.total"
                :show-total="(total, range) => `共 ${total-Math.ceil(total/ipagination.pageSize)} 条`">
                <template slot="buildOptionText" slot-scope="props">
                  <span>{{ props.value-1 }}条/页</span>
                </template>
              </a-pagination>
            </a-col>
          </a-row>
        </section>
        <!-- table区域-end -->
        <AccountModal ref="modalForm" @ok="modalFormOk" :oprType="'purchase'"></AccountModal>
        <import-file-modal ref="modalImportForm" @ok="modalFormOk"></import-file-modal>
        <!-- 表单区域 -->
        <debt-account-list ref="debtAccountList"></debt-account-list>
      </a-card>
    </a-col>
  </a-row>
</template>
<script>
import AccountModal from './modules/AccountModal'
import DebtAccountList from '@/views/report/modules/DebtAccountList'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getFormatDate, getPrevMonthFormatDate } from '@/utils/util'
import { findBySelectSup } from '@/api/api'
import JEllipsis from '@/components/jeecg/JEllipsis'
import moment from 'moment'
import { deleteAction, getAction, postAction } from '@/api/manage'
import ImportFileModal from '@/components/tools/ImportFileModal'
import { BillListMixin } from '@views/bill/mixins/BillListMixin'

export default {
    name: 'PurchaseAccount',
    mixins:[JeecgListMixin,BillListMixin],
    components: {
        DebtAccountList,
        JEllipsis,
        AccountModal,
        ImportFileModal
    },
    data () {
        return {
            labelCol: {
                span: 5
            },
            wrapperCol: {
                span: 18,
                offset: 1
            },
            // 查询条件
            queryParam: {
                statementType: '采购对账单',
                organId: '',
                beginTime: getPrevMonthFormatDate(3),
                endTime: getFormatDate(),
                createTimeRange: [moment(getPrevMonthFormatDate(3)), moment(getFormatDate())]
            },
            ipagination: {
                pageSize: 11,
                pageSizeOptions: ['11', '21', '31', '101', '201']
            },
            supList: [],
            firstTotal: '',
            lastTotal: '',
            tabKey: '1',
            // 表头
            columns: [
                {
                    title: '操作',
                    dataIndex: 'action',
                    align: 'center',
                    width: 180,
                    scopedSlots: { customRender: 'action' }
                },
                { title: '采购对账单', dataIndex: 'statementType', width: 150, ellipsis: true },
                { title: '统计类型 ', dataIndex: 'statisticalType', width: 150, ellipsis: true },
                { title: '工厂', dataIndex: 'factory', width: 150, ellipsis: true },
                { title: '供应商编号', dataIndex: 'supplierNumber', width: 100 },
                { title: '供应商名称', dataIndex: 'supplierName', width: 100 },
                { title: '订单编号', dataIndex: 'depotNumber', width: 100, ellipsis: true },
                { title: '行项目', dataIndex: 'lineProject', width: 100 },
                { title: '物料', dataIndex: 'barCode', width: 150, ellipsis: true },
                { title: '物料描述', dataIndex: 'materials', width: 100 },
                { title: 'FYrRef', dataIndex: 'fyrRef', width: 100 },
                { title: '参考凭证', dataIndex: 'referenceCertificate', width: 100 },
                { title: 'RfIt', dataIndex: 'rfIt', width: 150 },
                { title: '入库数量', dataIndex: 'materialCount', width: 100 },
                { title: '仍要开票数量', dataIndex: 'invoiceNum', width: 150 },
                { title: '订单单位', dataIndex: 'materialUnit', width: 100 },
                { title: '采购人员', dataIndex: 'orderPeople', width: 100 },
                { title: '不含税单价(本币)', dataIndex: 'unitPrice', width: 150 },
                { title: '仍要开票的净价值(本币)"', dataIndex: 'needInvoice', width: 100 },
                { title: '仍要开票价值（含税）', dataIndex: 'needInvoiceTax', width: 100 },
                { title: '货币单位 默认RMB', dataIndex: 'currencyType', width: 100 },
                { title: '租户ID', dataIndex: 'tenantId', width: 100 }
            ],
            url: {
                list: '/statement/list',
                importExcelUrl: '/statement/importPurchaseAccount',
                exportXlsUrl: '/statement/exportExcel',
                updateStatement:'/statement/updateStatement'
            }
        }
    },
    created () {
      this.initSystemConfig()
      this.initSupplier()
      this.loadData(1)
    },
    methods: {

      searchQuery () {
        this.loadData(1)
        this.getSystemConfig()
      },
      handleImportXls () {
        let importExcelUrl = this.url.importExcelUrl
        let templateUrl = '/doc/statement_template.xls'
        let templateName = '对账单Excel模板[下载]'
        this.$refs.modalImportForm.initModal(importExcelUrl, templateUrl, templateName)
        this.$refs.modalImportForm.title = '销售对账单导入'
      },
      /**
       * 更新对账单
       */
      updateStatement () {
        var that = this
        this.$confirm({
          title: '确认更新对账单',
          content: '是否更新对账单?',
          onOk: function () {
            that.loading = true
            postAction(that.url.updateStatement, {}).then((res) => {
              if (res.code === 200) {
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.data)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
        }
    }
}
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>
